//
//  NSAttributedString+MYGHTML.m
//  MYGrow
//
//  Created by 小火科技 on 2024/3/20.
//

#import "NSAttributedString+MYGHTML.h"

@implementation NSAttributedString (MYGHTML)
/**
 content：NSString 显示内容
 fontSize：int 字体大小
 color：UIColor 字体颜色
 needLine：BOOL 是否需要下划线
 isCenter：BOOL 是否居中
 witdh： 是html且包含图片时，图片显示最大宽度
 */
+(NSAttributedString *)fetchHtmlStringByContent:(NSString *)content fontSize:(int)fontSize color:(UIColor *)color needUserLine:(BOOL)needLine isCenter:(BOOL)isCenter htmlImageMaxWitdh:(CGFloat)witdh {
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
//    if(isCenter){
//        paragraphStyle.alignment = NSTextAlignmentCenter;
//    }
//    
//    NSMutableDictionary * attris = [NSMutableDictionary dictionaryWithDictionary:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:fontSize],NSForegroundColorAttributeName:color,NSParagraphStyleAttributeName:paragraphStyle}];
    NSMutableDictionary *attris = [NSMutableDictionary dictionary];
//    if(needLine){
//        [attris setObject:[NSNumber numberWithInteger:NSUnderlineStyleSingle] forKey:NSUnderlineStyleAttributeName];
//    }
    if([content containsString:@"<"] && [content containsString:@">"] && [content containsString:@"</"]){//html
        [attris setObject:NSHTMLTextDocumentType forKey:NSDocumentTypeDocumentAttribute];
        if(witdh>0){
            content = [NSString stringWithFormat:@"<head><style>img{width:auto;height:auto;max-width:%f}</style></head>%@",witdh,content];
        }
        NSMutableAttributedString * attribtStr = [[NSMutableAttributedString alloc] initWithData:[content dataUsingEncoding:NSUnicodeStringEncoding] options:attris documentAttributes:nil error:nil];
        return attribtStr;
    }else{
        NSAttributedString * attribtStr = [[NSAttributedString alloc]initWithString:content attributes:attris];
        return attribtStr;
    }
}

@end
